<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar
      title="京东注册"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <!-- 表单 -->
    <van-cell-group inset>
      <van-field
        v-model="sms"
        center
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
      >
        <template #button>
          <van-button size="small" type="primary" @click="submit"
            >发送验证码</van-button
          >
        </template>
      </van-field>
    </van-cell-group>

    <van-form @submit="next">
      <div style="margin: 16px">
        <van-button
          round
          block
          disabled
          type="danger"
          native-type="submit"
          v-if="sms.length !== 5"
        >
          下一步
        </van-button>
        <van-button round block type="danger" native-type="submit" v-else>
          下一步
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { dosendmsgcode } from "../../server/user.js";
export default {
  data() {
    return {
      sms: "",
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    submit() {
      dosendmsgcode({ tel: this.$store.state.username }).then((res) => {
        console.log(res);
        if (res.code === "200") {
          if (this.sms === res.data) this.next();
        }
      });
    },
    next() {
      this.$router.push("/password");
    },
  },
};
</script>

<style scoped>
.van-cell-group {
  margin-top: 6.25rem;
}
</style>
